<template>
  <el-dialog :title="`${form.scId ? '修改' : '添加'}安全文明`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item prop="scId" class="hide" />
      <el-form-item prop="parentId" label="上级">
        <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" show-count placeholder="选择上级" />
      </el-form-item>
      <el-form-item label="编码" prop="scCode"><el-input v-model.trim="form.scCode" placeholder="请输入安全文明编码" :maxlength="50" /></el-form-item>
      <el-form-item label="名称" prop="scName"><el-input v-model.trim="form.scName" placeholder="请输入安全文明名称" :maxlength="255" /></el-form-item>
      <el-form-item label="专业类别" prop="specialTypeId">
        <el-select v-model="form.specialTypeId" placeholder="请选择" default-first-option filterable style="width: 100%">
          <el-option v-for="dict in specialTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="scType">
        <el-select v-model="form.scType" placeholder="请选择" default-first-option filterable style="width: 100%">
          <el-option v-for="dict in cqCheckScTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :maxlength="255" /></el-form-item>
      <el-form-item label="排序" prop="seq"><el-input-number v-model="form.seq" :min="0" :max="1e8" :precision="0" controls-position="right" /></el-form-item>
      <el-form-item label="是否启用" prop="isEnable"><el-switch v-model="form.isEnable" /></el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改安全文明对话框
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { nameRule } from '@/utils/validate'
import { cqCheckScTypes } from '@/utils/baseData'
import { editFormMixin } from '@/utils/mixin'
import { getSpecialTypes } from '@/utils/dict/custom'
import { addCheckSc, updateCheckSc } from '@/api/qualityManage/cq'
export default {
  components: { Treeselect },
  mixins: [editFormMixin],
  data() {
    return {
      cqCheckScTypes,
      primaryKey: 'scId',
      addApi: addCheckSc,
      updateApi: updateCheckSc,
      form: {
        parentId: '',
        scId: '',
        scCode: '',
        scName: '',
        specialTypeId: '',
        scType: '',
        remark: '',
        seq: 0,
        isEnable: true
      },
      rules: {
        scCode: [{ required: true, message: '请输入安全文明编码' }, nameRule],
        scName: [{ required: true, message: '请输入安全文明名称' }, nameRule],
        specialTypeId: [{ required: true, message: '请选择专业类别' }],
        scType: [{ required: true, message: '请选择类型' }]
      },
      treeOptions: [],
      specialTypes: []
    }
  },
  created() {
    getSpecialTypes().then(res => (this.specialTypes = res))
  },
  methods: {
    opened(item, { treeData }) {
      this.treeOptions = [{ scId: '', scName: '主类目', children: treeData }]
    },
    normalizer(node) {
      if (!node.children?.length) delete node.children
      return { id: node.scId, label: node.scName, children: node.children }
    }
  }
}
</script>
